package com.cn.kabalifeuser.ui.integral.activity;

import com.cn.kabalifeuser.api.Api;
import com.cn.kabalifeuser.base.BasePresenter;
import com.cn.kabalifeuser.callback.CacheCallBack;
import com.cn.kabalifeuser.config.Constants;
import com.cn.kabalifeuser.ui.integral.bean.CommodityCommentListBean;
import com.cn.kabalifeuser.ui.integral.bean.DetailsInfoBean;
import com.cn.kabalifeuser.ui.integral.bean.OrderPlaceBean;
import com.cn.kabalifeuser.ui.login.bean.MsgBean;
import com.cn.kabalifeuser.ui.service.bean.BannerListBean;
import com.cn.kabalifeuser.utils.LoggerUtils;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.model.Response;

/**
 * Created by 晓林 on 2020/9/12.
 * function:
 */
public class IntegralCommodityDetailsPresenter extends BasePresenter<IntegralCommodityDetailsView> {


    public void onDetailsData(String token, int id) {
        OkGo.<DetailsInfoBean>get(Api.API_GOODS_DETAILS)
                .headers("token", token)
                .params("id", id)
                .execute(new CacheCallBack<DetailsInfoBean>() {
                    @Override
                    public void onSuccess(Response<DetailsInfoBean> response) {
                        super.onSuccess(response);
                        LoggerUtils.e("详情：" + response.body());
                        if (isViewAttached())
                            if (response.body().getCode() == Constants.HTTP_SUCCESS)
                                getView().onDetailsSuccess(response.body());
                            else if (response.body().getCode() == Constants.HTTP_AGAIN_LOGIN)
                                getView().onReLoggedIn(response.body().getMsg());
                            else
                                getView().onError(response.body().getMsg());
                    }
                });
    }

    /**
     * 下单。提交订单。
     *
     * @param token
     * @param type
     * @param good_id
     * @param spec_id
     * @param good_num
     */
    public void onOrderData(String token, String type, int good_id, int spec_id, int good_num) {
        OkGo.<OrderPlaceBean>post(Api.API_ORDER_CART)
                .headers("token", token)
                .params("type", type)
                .params("good_id", good_id)
                .params("spec_id", spec_id)
                .params("good_num", good_num)
                .execute(new CacheCallBack<OrderPlaceBean>() {
                    @Override
                    public void onSuccess(Response<OrderPlaceBean> response) {
                        super.onSuccess(response);
                        LoggerUtils.e("下单：" + response.body());
                        if (isViewAttached())
                            if (response.body().getCode() == Constants.HTTP_SUCCESS)
                                getView().onOrderSuccess(response.body());
                            else if (response.body().getCode() == Constants.HTTP_AGAIN_LOGIN)
                                getView().onReLoggedIn(response.body().getMsg());
                            else
                                getView().onError(response.body().getMsg());
                    }
                });
    }

    /**
     * 加入购物车
     * @param token
     * @param type
     * @param good_id
     * @param spec_id
     * @param good_num
     */
    public void onAddCartData(String token, String type, int good_id, int spec_id, int good_num) {
        OkGo.<MsgBean>post(Api.API_ORDER_CART_UP)
                .headers("token", token)
                .params("type", type)
                .params("good_id", good_id)
                .params("spec_id", spec_id)
                .params("num", good_num)
                .execute(new CacheCallBack<MsgBean>() {
                    @Override
                    public void onSuccess(Response<MsgBean> response) {
                        super.onSuccess(response);
                        LoggerUtils.e("加入购物车：" + response.body());
                        if (isViewAttached())
                            if (response.body().getCode() == Constants.HTTP_SUCCESS)
                                getView().onAddCartSuccess(response.body());
                            else if (response.body().getCode() == Constants.HTTP_AGAIN_LOGIN)
                                getView().onReLoggedIn(response.body().getMsg());
                            else
                                getView().onError(response.body().getMsg());
                    }
                });
    }

    /**
     * 评论
     *
     * @param token
     * @param id
     */
    public void onDetailsCommentsData(String token, int id) {
        OkGo.<CommodityCommentListBean>get(Api.API_GOODS_DETAILS_COMMENT)
                .headers("token", token)
                .params("good_id", id)
                .params("limit", 2)
                .params("page", 1)
                .execute(new CacheCallBack<CommodityCommentListBean>() {
                    @Override
                    public void onSuccess(Response<CommodityCommentListBean> response) {
                        super.onSuccess(response);
                        LoggerUtils.e("详情comment：" + response.body());
                        if (isViewAttached())
                            if (response.body().getCode() == Constants.HTTP_SUCCESS)
                                getView().onDetailsCommentsSuccess(response.body());
                            else if (response.body().getCode() == Constants.HTTP_AGAIN_LOGIN)
                                getView().onReLoggedIn(response.body().getMsg());
                            else
                                getView().onError(response.body().getMsg());
                    }
                });
    }

}
